package org.barad.architecture.analyzer.archpad.region;

import java.awt.Color;
import java.util.LinkedHashSet;
import java.util.List;

import javax.swing.JOptionPane;

import org.barad.architecture.analyzer.archpad.ArchPad;
import org.barad.architecture.analyzer.archpad.component.ArchPadComponent;
import org.barad.architecture.analyzer.archpad.component.ColorIterator;
import org.jgraph.graph.DefaultGraphCell;

/**
 * This class manages the architectural regions in ArchPad.
 * 
 * @author svetoslavganov@gmail.com (Svetoslav R. Ganov)
 *
 */
public class RegionManager {
	private LinkedHashSet<Region> regions;
	private ColorIterator colorIterator;
	private boolean modified;
	private ArchPad archPad;

	/**
	 * Creates a new instance.
	 * 
	 * @param archPad The {@link ArchPad} instance.
	 */
	public RegionManager(ArchPad archPad) {
		this.archPad = archPad;
		this.colorIterator = new ColorIterator(50, new int[]{200, 200, 200});
		this.regions = new LinkedHashSet<Region>();
		this.modified = false;
	}
	
	/**
	 * Clears all architectural regions.
	 */
	public void clearRegions() {
		regions.clear();
		archPad.resetRegionPanel();
	}
	
	/**
	 * Creates an architectural region.
	 */
	public void createRegion() {
		//get selected cells
		Object[] selected = archPad.getCustomizedJGraph().getSelectionCells();
		Object[] cells = archPad.getCustomizedJGraph().getDescendants(selected);
		//get a name for the region
		String message = "Please, provide a name for the region";
		String name = JOptionPane.showInputDialog(message, "New region");
		if (name != null) {
			//get color for the region
			if (!colorIterator.hasNext()) {
				colorIterator.reset();
			}
			Color color = colorIterator.next();
			Region region = new Region(name); 
			region.setColor(color);
			regions.add(region);
			for (Object obj: cells) {
				if (obj.getClass().equals(DefaultGraphCell.class)) {
					DefaultGraphCell cell = (DefaultGraphCell)obj;
					ArchPadComponent archPadComponent = (ArchPadComponent)cell.getUserObject();
					archPadComponent.getRegions().add(region);
				}
			}
			archPad.addRegionJCheckBox();
			archPad.getCustomizedJGraph().repaint();
			modified = true;
		}
	}
	
	/**
	 * Load the architectural {@link Region}s from an array
	 * of graph cells.
	 * 
	 * @param cells The cells.
	 */
	public void loadRegions(Object[] cells) {
		for (Object obj: cells) {
			DefaultGraphCell cell = (DefaultGraphCell)obj;
			ArchPadComponent archPadComponent = (ArchPadComponent)cell.getUserObject();
			regions.addAll(archPadComponent.getRegions());
			List<?> children = cell.getChildren();
			if (children.size() > 0) {
				loadRegions(children.toArray());
			}
		}
	}
	
	/**
	 * Removes an architectural region.
	 */
	public void removeRegion() {
		//TODO: Implement
		modified = true;
	}

	/**
	 * Gets the architectural {@link Region}s.
	 * 
	 * @return The {@link Region}s.
	 */
	public LinkedHashSet<Region> getRegions() {
		return regions;
	}

	/**
	 * Sets the architectural {@link Region}s.
	 * 
	 * @param regions The {@link Region}s.
	 */
	public void setRegions(LinkedHashSet<Region> regions) {
		this.regions = regions;
	}
	
	/**
	 * Gets the {@link ColorIterator}.
	 * 
	 * @return The {@link ColorIterator}.
	 */
	public ColorIterator getColorIterator() {
		return colorIterator;
	}

	public boolean isModified() {
		return modified;
	}
}
